<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class Article extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {   $name= $request->param();
//    var_dump($name);die;
        $data = db('article as a,s75_data as d')->field('a.id,a.cid,d.name dname,a.name,content')
            ->where('d.id = a.cid')->where('author',$name['name'])->select();

        return view('article/list', ['title'=>'编辑文章','data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        return view('article/add', ['title'=>'添加文章']);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 新增数据
        $post = $request->post();
        $res = db('data')->field(['id'])->where('name',$post['dname'])->find();
        if (empty($res)) {
            return $this->error('亲,该书不存在哦');
        }
        $data['cid'] = $res['id'];
        $data['name'] = $post['name'];
        $data['content'] = $post['editorValue'];

        $res = db('article')->insert($data);
        if($res){ return $this->success('添加成功',url('admin/Article/index')); }else{ return $this->error('添加失败'); }


    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = db('article as a,s75_data as d')->field('a.id,a.cid,d.name dname,a.name,content')
            ->where('d.id = a.cid')->where('a.id',$id)->find();

        return view('article/edit', ['title'=>'编辑文章','data'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->put();

        $data['id'] = $id;
        $data['content'] = $data['editorValue'];
        $data =array_diff_key($data, ['_method' => "Put"]);
        $data =array_diff_key($data, ['editorValue' => ""]);
        $data =array_diff_key($data, ['dname' => ""]);

        $res = db('article')->update($data);
        if($res){ return $this->success('修改成功',url('admin/article/index')); }else{ return $this->error('修改失败'); }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = db('article')->delete($id);

        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的文章删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }

        // echo json_encode($result);
        return json($info);
    }




}


?>